/*
 * LoginFrame.java
 *
 * Created on 15 april 2008, 15:11
 */

package be.hogent.iii.project.client.gui.login;

import be.hogent.iii.project.client.netwerk.Connectie;
import be.hogent.iii.project.common.bericht.login.LoginGegevensBericht;
import com.thoughtworks.xstream.XStream;
import java.awt.Color;

/**
 *
 * @author  Caribou
 */
public class LoginFrame extends javax.swing.JFrame {
    
    private static LoginFrame frame;
    private Connectie connectie;
    private String ip;
    private XStream xStream;
    
    /** Creates new form LoginFrame */
    private LoginFrame() {
        initComponents();
        setLocationRelativeTo(null);
        ip = "127.0.0.1";
        connectie = new Connectie(ip);
        xStream = new XStream();
    }

    public static synchronized LoginFrame getLoginFrame() {
        if (frame == null) {
                frame = new LoginFrame();
        }
        return frame;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        loginTxt = new javax.swing.JTextField();
        wachtwoordPwd = new javax.swing.JPasswordField();
        loginLbl = new javax.swing.JLabel();
        wachtwoordLbl = new javax.swing.JLabel();
        loginBtn = new javax.swing.JButton();
        wachtwoordVergetenBtn = new javax.swing.JButton();
        nieuwGebruikerBtn = new javax.swing.JButton();
        labelFouteLogin = new javax.swing.JLabel();
        labelIp = new javax.swing.JLabel();
        buttonVeranderIp = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login");
        setBackground(new java.awt.Color(0, 0, 0));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(new java.awt.Color(0, 0, 0));
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));

        loginTxt.setBackground(new java.awt.Color(102, 0, 0));
        loginTxt.setFont(new java.awt.Font("Tahoma", 1, 11));
        loginTxt.setForeground(new java.awt.Color(204, 153, 0));
        loginTxt.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        loginTxt.setToolTipText("Geef hier je gebruikersnaam in. ");
        loginTxt.setCaretColor(new java.awt.Color(204, 153, 0));
        loginTxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginTxtActionPerformed(evt);
            }
        });

        wachtwoordPwd.setBackground(new java.awt.Color(102, 0, 0));
        wachtwoordPwd.setFont(new java.awt.Font("Tahoma", 1, 11));
        wachtwoordPwd.setForeground(new java.awt.Color(204, 153, 0));
        wachtwoordPwd.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        wachtwoordPwd.setCaretColor(new java.awt.Color(204, 153, 0));
        wachtwoordPwd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wachtwoordPwdActionPerformed(evt);
            }
        });

        loginLbl.setFont(new java.awt.Font("Tahoma", 1, 11));
        loginLbl.setForeground(new java.awt.Color(255, 153, 0));
        loginLbl.setText("Login: ");

        wachtwoordLbl.setFont(new java.awt.Font("Tahoma", 1, 11));
        wachtwoordLbl.setForeground(new java.awt.Color(255, 153, 0));
        wachtwoordLbl.setText("Wachtwoord: ");

        loginBtn.setBackground(new java.awt.Color(102, 0, 0));
        loginBtn.setForeground(new java.awt.Color(204, 153, 0));
        loginBtn.setText("Login");
        loginBtn.setToolTipText("Klik om in te loggen. ");
        loginBtn.setAlignmentY(0.0F);
        loginBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginBtnActionPerformed(evt);
            }
        });

        wachtwoordVergetenBtn.setBackground(new java.awt.Color(102, 0, 0));
        wachtwoordVergetenBtn.setForeground(new java.awt.Color(204, 153, 0));
        wachtwoordVergetenBtn.setText("Wachtwoord vergeten");
        wachtwoordVergetenBtn.setAlignmentY(0.0F);
        wachtwoordVergetenBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wachtwoordVergetenBtnActionPerformed(evt);
            }
        });

        nieuwGebruikerBtn.setBackground(new java.awt.Color(102, 0, 0));
        nieuwGebruikerBtn.setForeground(new java.awt.Color(204, 153, 0));
        nieuwGebruikerBtn.setText("Nieuwe gebruiker");
        nieuwGebruikerBtn.setAlignmentY(0.0F);
        nieuwGebruikerBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nieuwGebruikerBtnActionPerformed(evt);
            }
        });

        labelFouteLogin.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelFouteLogin.setForeground(new java.awt.Color(204, 0, 0));

        labelIp.setFont(new java.awt.Font("Tahoma", 2, 11));
        labelIp.setForeground(new java.awt.Color(255, 153, 0));
        labelIp.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelIp.setText("127.0.0.1");

        buttonVeranderIp.setBackground(new java.awt.Color(102, 0, 0));
        buttonVeranderIp.setForeground(new java.awt.Color(204, 153, 0));
        buttonVeranderIp.setText("Verander");
        buttonVeranderIp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonVeranderIpActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(labelIp, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonVeranderIp, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(nieuwGebruikerBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(wachtwoordVergetenBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(loginBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelFouteLogin, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(loginLbl)
                                    .addComponent(wachtwoordLbl))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(wachtwoordPwd, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                                    .addComponent(loginTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE))))))
                .addGap(19, 19, 19))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginLbl)
                    .addComponent(loginTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(wachtwoordPwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(wachtwoordLbl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelFouteLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(loginBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(wachtwoordVergetenBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nieuwGebruikerBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(labelIp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonVeranderIp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {loginBtn, nieuwGebruikerBtn, wachtwoordVergetenBtn});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loginBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginBtnActionPerformed
        String login = loginTxt.getText();
        
        if(login.equals("")) {
            setLabelFout("Vul beide velden in aub");
        }
        else { //beiden zijn ingevuld
            LoginGegevensBericht loginMsg = new LoginGegevensBericht(loginTxt.getText(), wachtwoordPwd.getPassword());
            connectie.stuurBericht(xStream.toXML(loginMsg));  
        }
}//GEN-LAST:event_loginBtnActionPerformed

    private void wachtwoordVergetenBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wachtwoordVergetenBtnActionPerformed
        NieuwWachtwoordFrame.getNieuwWachtwoordFrame().setVisible(true);
    }//GEN-LAST:event_wachtwoordVergetenBtnActionPerformed

    private void nieuwGebruikerBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nieuwGebruikerBtnActionPerformed
        NieuwGebruikerFrame.getNieuwGebruikerFrame().setVisible(true);
    }//GEN-LAST:event_nieuwGebruikerBtnActionPerformed

    private void loginTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginTxtActionPerformed
        loginBtnActionPerformed(evt);
    }//GEN-LAST:event_loginTxtActionPerformed

    private void wachtwoordPwdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wachtwoordPwdActionPerformed
        loginBtnActionPerformed(evt);
    }//GEN-LAST:event_wachtwoordPwdActionPerformed

    private void buttonVeranderIpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonVeranderIpActionPerformed
        // TODO add your handling code here:
        VeranderIP veranderIp = new VeranderIP(ip);
        veranderIp.setVisible(true);
}//GEN-LAST:event_buttonVeranderIpActionPerformed
    
    public void setLabelFout(String msg) {
        labelFouteLogin.setForeground(Color.RED);
        labelFouteLogin.setText(msg);
        //labelFouteLogin.repaint();
        //labelFouteLogin.revalidate();
        wachtwoordPwd.setText("");
    }
    
    public void setLabelGoed(String msg) {
        labelFouteLogin.setForeground(Color.GREEN);
        labelFouteLogin.setText(msg);
        //labelFouteLogin.repaint();
        //labelFouteLogin.revalidate();
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                LoginFrame.getLoginFrame().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonVeranderIp;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel labelFouteLogin;
    private javax.swing.JLabel labelIp;
    private javax.swing.JButton loginBtn;
    private javax.swing.JLabel loginLbl;
    private javax.swing.JTextField loginTxt;
    private javax.swing.JButton nieuwGebruikerBtn;
    private javax.swing.JLabel wachtwoordLbl;
    private javax.swing.JPasswordField wachtwoordPwd;
    private javax.swing.JButton wachtwoordVergetenBtn;
    // End of variables declaration//GEN-END:variables

    public Connectie getConnectie() {
        return connectie;
    }

    public void setConnectie(Connectie connectie) {
        this.connectie = connectie;
    }
    
    public void nieuweServer(String ip) {
        this.ip = ip;
        this.labelIp.setText(this.ip); 
        //this.labelIp.revalidate();
        //this.labelIp.repaint();
        this.connectie.sluitVerbinding(); //degene die we nu hebben afsluiten
        //en een nieuwe aanmaken
        this.labelFouteLogin.setForeground(Color.WHITE);
        this.labelFouteLogin.setText("Proberen te verbinden met " + ip);
        this.connectie = new Connectie(this.ip);   
    }
    
    public void setIp(String ip) {
        this.ip = ip;
    }
    
    public String getIP() {
        return ip;
    }
    
}
